package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.questionbank.ScoringRuleStatus;
import com.edu.exam.enums.questionbank.ScoringRuleType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 多选题评分规则实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：mcq_scoring_rules
 *
 * 管理多选题的评分配置信息，包含规则类型、分数设置、部分得分和倒扣分配置、计算公式以及详细的规则参数。
 * 支持灵活的评分策略配置，包括全对得分、部分得分、倒扣分等多种评分模式。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("多选题评分规则表")
@SQLDelete(sql = "UPDATE mcq_scoring_rules SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class MultipleChoiceScoringRule {

    /**
     * 评分规则唯一标识符
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 关联多选题ID
     */
    @NotNull(message = "多选题ID不能为空")
    @Column(name = "question_id", nullable = false)
    private Long questionId;

    /**
     * 规则类型
     */
    @NotNull(message = "规则类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "rule_type", nullable = false, length = 50)
    private ScoringRuleType ruleType;

    /**
     * 题目满分
     */
    @NotNull(message = "满分不能为空")
    @Column(name = "full_score", nullable = false, precision = 5, scale = 2)
    private BigDecimal fullScore;

    /**
     * 是否允许部分得分
     */
    @NotNull(message = "部分得分设置不能为空")
    @Column(name = "partial_score_enabled", nullable = false)
    @Builder.Default
    private Boolean partialScoreEnabled = true;

    /**
     * 是否启用倒扣分
     */
    @NotNull(message = "倒扣分设置不能为空")
    @Column(name = "penalty_enabled", nullable = false)
    @Builder.Default
    private Boolean penaltyEnabled = false;

    /**
     * 最少正确选项数要求
     */
    @Column(name = "min_correct_options")
    private Integer minCorrectOptions;

    /**
     * 得分计算公式描述
     */
    @Size(max = 200, message = "计算公式长度不能超过200个字符")
    @Column(name = "score_calculation", length = 200)
    private String scoreCalculation;

    /**
     * 规则详细描述
     */
    @Column(name = "rule_description", columnDefinition = "TEXT")
    private String ruleDescription;

    /**
     * 规则参数，JSON格式存储
     */
    @Column(name = "rule_parameters", columnDefinition = "TEXT")
    private String ruleParameters;

    /**
     * 规则状态
     */
    @NotNull(message = "规则状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Builder.Default
    private ScoringRuleStatus status = ScoringRuleStatus.DRAFT;

    /**
     * 创建者ID
     */
    @NotBlank(message = "创建者ID不能为空")
    @Size(max = 32, message = "创建者ID长度不能超过32个字符")
    @Column(name = "creator_id", nullable = false, length = 32)
    private String creatorId;

    /**
     * 创建时间
     */
    @NotNull(message = "创建时间不能为空")
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @NotNull(message = "更新时间不能为空")
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 软删除标记
     */
    @NotNull(message = "删除标记不能为空")
    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    /**
     * 删除时间
     */
    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createTime = now;
        this.updateTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updateTime = LocalDateTime.now();
    }


}